/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.parameter_constraints;

import picounit.util.Equal;
import picounit.verify.constraint.EqualUtil;


public class InstanceOfInvocationHandler<T> extends EqualsInvocationHandler
	implements Equal<InstanceOfInvocationHandler<T>>{

	private final Class<T> instanceOfWhat;

	public InstanceOfInvocationHandler(Class<T> instanceOfWhat) {
		this.instanceOfWhat = instanceOfWhat;
	}

	public boolean matches(Class<T> instanceOfWhat) {
		return this.instanceOfWhat.equals(instanceOfWhat);
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// EqualsInvocationHandler methods
	///////////////////////////////////////////////////////////////////////////////////////////////
	
	@Override
	public boolean isEqual(Object object) {
		return instanceOfWhat.isAssignableFrom(object.getClass());
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Equal methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	public boolean equalTo(InstanceOfInvocationHandler<T> other) {
		return other.matches(instanceOfWhat);
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Object methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public boolean equals(Object object) {
		return EqualUtil.isEqual(this, object);
	}

	@Override
	public String toString() {
		return "instanceOf<" + instanceOfWhat.getName() + ">";
	}
}
